﻿using animexx.Models;
using animexx.OAuth;
using AsyncOAuth;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace animexx.ViewModels
{
    class ENSViewModel : ViewModelBase
    {
        public ENSViewModel()
        {
        }

        public async void LoadData(string id)
        {
            var client = new HttpClient(new OAuthMessageHandler(Keys.ConsumerKey, Keys.ConsumerSecret, App.accessToken));
            var json = await client.GetStringAsync(Urls.BaseUrl + "json/ens/ens_open?text_format=html&ens_id=" + id);
            System.Diagnostics.Debug.WriteLine("ens:" + json);
            JSONReply<ENS> reply = JsonConvert.DeserializeObject<JSONReply<ENS>>(json);
            _data = reply.returnValue;

            this.IsDataLoaded = true;
            OnPropertyChanged("Data");
            OnPropertyChanged("An");
        }

        private ENS _data;
        public ENS Data
        {
            get { return _data; }
            set {
                SetProperty(ref _data, value);
                OnPropertyChanged("An");
            }
        }
        public string An
        {
            get
            {
                if (_data == null)
                    return "";
                List<string> names = new List<string>();
                foreach (UserObject user in _data.an)
                {
                    names.Add(user.username);
                }
                return string.Join(", ", names);
            }
        }

    }
}
